#!/bin/sh

USERS=
_UID=0
File=./000.txt
# 给这个文本定义一个变量
Files=`cat ./000.txt`
for a in $Files
# 给数组定义一个变量
do
       USERS[$b]=$a
       _UID=$(($b+1))
done
function add_user {
# 定义添加用户的函数
while true
do
    echo -n "输入姓名:"
    read name
    echo -n "输入性别:"
    read gender
    echo -n "输入年龄:"
    read age

    user_info="$name:$gender:$age"
    USERS[$_UID]=$user_info
    _UID=$(($_UID+1))
    echo "There are ${#USERS[@]} users"
    # 输出用户数量
    echo -n "是否继续添加(y\n):"
    read answer
    if [ $answer == "n" -o $answer == "N" ];then
        break
    fi
done
}
function query_user {
    echo "请输入姓名"
    read name
    name1=$name
    for x in ${USERS[@]}
    do
        Y=`echo ${x} | awk -F":" '{print $1}'`
        # 定义一个函数
    	if [ $Y == "$name1" ];then
         echo ${x}
         break;
        fi
done
}
function update_user {
    echo -n "请输入需要更新学员的姓名:"
    read name
    for x in ${USERS[@]}
    do
        name2=`echo ${x} | awk -F":" '{ print $1 }'`
        if [ $name2 == "$name" ];then
                echo -e "请按以下格式输入更改信息(姓名:性别:年龄) :"
                read name3
                USERS[$x1]="$name3";
         else
                x1=$(($x1+1))
               # 循环输出
        fi
    done

}

while true;
do
    echo "1. 添加学员"
    echo "2. 查询学员"
    echo "3. 更新学员"
    echo "4. 退出系统"

    echo -n "请选择:"
    read choice

    if [ $choice -eq 1 ]; then
        add_user
    elif [ $choice -eq 2 ]; then
        query_user
    elif [ $choice -eq 3 ]; then
        update_user
    elif [ $choice -eq 4 ]; then
        echo "是否保存信息(y\n):"
        read d
            if [ $d == "y" ];then
            cat /dev/null > $File
            # 清空
            for ((i=0;$i<$_UID;i++))
            do
              echo ${USERS[$i]} >> $File
           # 保存到文本
            done
           exit 0  
    elif [ $d =="n"];then
          exit
    else
        echo "错误的选择!!!"
            fi
    fi
 done
